package com.example.task1.ui.AllService.PublicBenefit;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PublicLogBean;
import com.example.task1.bean.PublicRecommendBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class PublicDetailsActivity extends AppCompatActivity {

    private RecyclerView rvList;
    private int id;
    private int pageNum = 1;
    private PublicListAdapter recommendListAdapter;
    private LinearLayoutManager linearLayoutManager;
    private boolean isTrue = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_public_details);
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setTitle("捐款记录");
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        initView();
        id = getIntent().getIntExtra("id", 0);
        setAdapter();
        initData();
        business();
        initView();
        initView();
    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    private void business() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (recommendListAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    initData();
                }
            }
        });
    }

    private void initData() {
        Call<ApiResp> apiRespCall = Api.api().get(MyApplication.TOKEN, "/prod-api/api/public-welfare/donate-record/list?activityId=" + id + "&pageSize=10&pageNum=" + pageNum);
        Api.send(apiRespCall, PublicLogBean.class, new CallBack<PublicLogBean>() {

            @Override
            public void success(PublicLogBean data) {
                pageNum++;
                if (recommendListAdapter.getData().size() < data.getTotal()) {
                    recommendListAdapter.setData(data.getRows());
                    isTrue = true;
                } else {
                    MyApplication.show("没有个更多数据了");
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }

    private void setAdapter() {
        recommendListAdapter = new PublicListAdapter(PublicDetailsActivity.this);
        linearLayoutManager = new LinearLayoutManager(PublicDetailsActivity.this);
        rvList.setLayoutManager(linearLayoutManager);
        rvList.setAdapter(recommendListAdapter);
    }

    private void initView() {
        rvList = (RecyclerView) findViewById(R.id.rv_list);
    }
}